﻿using System.Collections.Generic;
using System.Linq;

namespace ReadyApps.Data.Wiki
{
    /// <summary>
    /// Represents the content and links of a particular page and revision.
    /// </summary>
    /// <seealso cref="InternalPageLink"/>
    public class PageContent
    {
        #region Variables and Constants

        private List<InternalPageLink> internalLinks;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the page title.
        /// </summary>
        public string Title { get; private set; }

        /// <summary>
        /// Gets the textual content of the page.
        /// </summary>
        public string Text { get; private set; }

        /// <summary>
        /// Gets an <see cref="IEnumerable{InternalPageLinks}"/> of links to other pages within
        /// the same site.
        /// </summary>
        public IEnumerable<InternalPageLink> Links
        {
            get { return internalLinks.AsReadOnly(); }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Creates a new <see cref="PageContent"/> object with the supplied title, text content
        /// and optionally an <see cref="IEnumerable{InternalPageLinks}"/> to start with.
        /// </summary>
        /// <param name="title">The title of the page.</param>
        /// <param name="text">The textual content of the page.</param>
        /// <param name="links">An optional collection of links to other pages.</param>
        public PageContent(string title, string text, IEnumerable<InternalPageLink> links)
        {
            Title = title;
            Text = text;
            internalLinks = new List<InternalPageLink>();
            if (links != null)
            {
                internalLinks.AddRange(links);
            }
        }

        #endregion

        #region Method Overrides

        public override int GetHashCode()
        {
            unchecked // Don't worry about overflowing integer limits
            {
                int hash = 17;
                hash = hash * 23 + Title.GetHashCode();
                hash = hash * 23 + Text.GetHashCode();
                foreach (var link in Links)
                {
                    hash = hash * 23 + link.GetHashCode();
                }
                return hash;
            }
        }

        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }

            PageContent other = (PageContent)obj;
            return GetHashCode() == other.GetHashCode();
        }

        #endregion
    }
}
